/**
 * 敌人小兵的对象
 */
 class Enemy {
    constructor() {
        // 纵坐标产生300~700的随机数
        this.y = (Math.random() * 300 + 400) - 88;
        this.x = screen.availWidth;
        this.imgs = resObj.slice(29, 45);
        // 所有的图片大小都是一样的，所以随便取一张图片做为当前对象的宽度与高度
        this.width = this.imgs[0].width;
        this.height = this.imgs[0].height;
        // 随机产生一个速度，让这个敌人移动
        this.speed = Math.random() * 2 + 2;
        this.imgIndex = 0;
        this.hp = 100;
        this.totalHp = 100;
    }
    
    
    move() {

        // 左负右正，上负下正
        this.x -= this.speed;
        //TODO:思考一下，如果当前敌人的横坐标小于0了，怎么处理？
        if (this.x < -this.width) {
            var index = enemyList.indexOf(this);
            enemyList.splice(index, 1);
        }
    }
    draw(ctx) {
        ctx.strokeRect(this.x - 10, this.y - 18, this.width, 10);
        ctx.fillStyle = 'red';
        ctx.fillRect(this.x - 10, this.y - 18, this.hp * this.width / this.totalHp, 10);
        ctx.font = '12px';
        ctx.textAlign = 'center';
        ctx.fillStyle = 'black';
        ctx.fillText(this.hp + '/' + this.totalHp, this.x + this.width / 2, this.y - 20 );
        this.move(); 
        ctx.drawImage(this.imgs[parseInt(this.imgIndex)], this.x, this.y, this.width, this.height);
        this.imgIndex += 0.5;
        if (this.imgIndex >= this.imgs.length) {
            this.imgIndex = 0;
        }       
        // if (beAttack == true) {

        // }                                     
    }
    
}